package ch.mylonas.saometer.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
import ch.mylonas.saometer.data.SerialData;
import ch.mylonas.saometer.data.processing.Punch;
import ch.mylonas.saometer.data.processing.PunchDetection;
import ch.mylonas.saometer.visualization.RollingGraph;
import ch.mylonas.saometer.visualization.PunchGraph;

public class SaoMeter extends PApplet{
	
	private static final long serialVersionUID = 1L;
	
	// colors used
	int backgroundColor = color(0, 0, 0);
	int lineColorX = color(0,180,0);
	int lineColorY = color(180,0,180);
//	int lineColorZ = color(100,255,255);
	
	// the three rolling-graph windows
	RollingGraph rollingGraphX;
	RollingGraph rollingGraphY;
//	RollingGraph rollingGraphZ;
	
	// the punch-graph
	PunchGraph punchGraph;
	
	SerialData serialData;
	PunchDetection punchDetection;
//	SaoMeterConfig saoMeterConfig;
	Punch currentPunch = null;
	
	PImage backgroundImg;
	
	public SaoMeter() {
		super();

	    String[] contextPaths = new String[] {"applicationContext.xml"};
        ApplicationContext ctx = new ClassPathXmlApplicationContext(contextPaths);
//        saoMeterConfig = (SaoMeterConfig) ctx.getBean("saoMeterConfig");        
        serialData = (SerialData) ctx.getBean("serialData");
     
        punchDetection = (PunchDetection) ctx.getBean("punchDetection");
        
        rollingGraphX = (RollingGraph) ctx.getBean("rollingGraphX");
        rollingGraphY = (RollingGraph) ctx.getBean("rollingGraphY");
 //       rollingGraphZ = (RollingGraph) ctx.getBean("rollingGraphZ");
        
        punchGraph = (PunchGraph) ctx.getBean("punchGraph");
        
	}

	public void setup() {
		
		// the Processing-settings
//	    size(screen.width, screen.height);
	    size(1024, 768);
//		size(800, 600);
	    
	    PFont font = createFont("Verdana", 20);
	    textFont(font);
	    
	    colorMode(RGB, 1);
	    smooth();    
	    frameRate(80);
	    
	    backgroundImg = loadImage("saometer_background.png");
	    
	    // initiates the serial conncetions
        serialData.setMyParent(this);
	    
	    // setup the RollingGraph
	    this.rollingGraphX.setColor(this.lineColorX);
	    this.rollingGraphX.setParent(this);
//	    this.rollingGraphX.setBackgroundColor(this.backgroundColor);
	    
	    this.rollingGraphY.setColor(this.lineColorY);
	    this.rollingGraphY.setParent(this);
//	    this.rollingGraphY.setBackgroundColor(this.backgroundColor);
	    
	    // setup the punchGraph
	    this.punchGraph.setColor(this.lineColorX);
	    this.punchGraph.setParent(this);
	    
	}
	
	 public void draw() {
//		background(backgroundColor);
		
		image(backgroundImg, 0, 0);
		
		smooth();
		
		this.rollingGraphX.draw(serialData.getData(0));
		this.rollingGraphY.draw(serialData.getData(1));
//		this.rollingGraphZ.draw(serialData.getData(2));
		
		Punch punch = this.punchDetection.detectPunch(serialData);
		
		if(punch != null){
			System.out.println("punch!!");
			System.out.println("max: " + punch.getMaxAcceleration());
			System.out.println("sum: " + punch.getAcceleration());
			
			this.currentPunch = punch;
			
		}
		
		if(this.currentPunch != null){
			this.punchGraph.draw(this.currentPunch);
			this.text(this.currentPunch.getAcceleration(), 863, 210);
			this.text(this.currentPunch.getMaxAcceleration(), 863, 323);
		}
		
	 }
	
	static public void main(String[] args) {
		
//		PApplet.main(new String[] {"--present", "ch.mylonas.saometer.main.SaoMeter"});
		PApplet.main(new String[] {"ch.mylonas.saometer.main.SaoMeter"});
	}  
	
}
